package com.zzld.modules.autoinn.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.abel533.entity.Example;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zzld.core.service.BaseServiceImpl;
import com.zzld.modules.autoinn.entity.Collar;
import com.zzld.modules.autoinn.entity.Coupon;
import com.zzld.modules.autoinn.mapper.CollarMapper;
import com.zzld.modules.autoinn.mapper.CouponMapper;
import com.zzld.modules.autoinn.service.IConponService;
import com.zzld.modules.common.dto.TreeGrid;

@Service("conponService")
public class ConponServiceImpl extends BaseServiceImpl<Coupon>  implements IConponService{
	
	@Autowired
	private CouponMapper couponMapper;

	@Autowired
	private CollarMapper collarMapper;
	
	public PageInfo list(Map<String,Object> map,int PageNum,int PageSize){
		// 以name字段升序排序
		PageHelper.startPage(PageNum, PageSize);
		List<Coupon> list = couponMapper.selectbypage(map);
		for(int i=0;i<list.size();i++){
			Example ex = new Example(Collar.class);
			ex.createCriteria().andEqualTo("counponId", list.get(i).getCouponId());
			List<Collar> list1 = collarMapper.selectByExample(ex);
			if(list1!=null){
				list.get(i).setCollarcount(list1.size());
			}else{
				list.get(i).setCollarcount(0);
			}
		}
		PageInfo page = new PageInfo(list);

		return page;
	}
	
	public List<Coupon> selectbyjuan(Map<String,Object> map){
	    try{
	    	return couponMapper.selectbyjuan(map);
	    }catch(Exception e){
	    	e.printStackTrace();
	    }
	    return null;
	}
	
	public List<Coupon> selectbyke(Map<String,Object> map){
		return couponMapper.selectbyke(map);
	}
	
	public List<Coupon> selectbyzhifu(Map<String,Object> map){
		return couponMapper.selectbyzhifu(map);
	}
	
	public Coupon selectbycolarname(String id) {
		return couponMapper.selectbycolarname(id);
	}
	
	/**
	 * 获取树形网格
	 * 
	 * @return
	 */
	public List<TreeGrid> treeGrid() {
		List<TreeGrid> treeGrids = new ArrayList<TreeGrid>();
	
        Map<String,Object> map =new HashMap<String,Object>();
        map.put("nowtime", new Date());
        map.put("couponZhi", 3);
	     	
		List<Coupon> licoupon = couponMapper.selectbyjuan(map);
		if (licoupon != null && licoupon.size() > 0) {
			for (Coupon res : licoupon) {
				TreeGrid treeGrid = new TreeGrid();
				BeanUtils.copyProperties(res, treeGrid);
				treeGrid.setText(res.getCouponName());
				treeGrid.setId(res.getCouponId());
				treeGrids.add(treeGrid);
			}
		}
		return treeGrids;
	}
	
}
